﻿using System;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using TS.DataAccess;
using TS.Entities;

namespace TS.Bussiness
{
    public class NumberBikeBussiness
    {
        /// <summary>
        /// Select Gateway Pagging
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strFilter"></param>
        /// <param name="strOrderBy"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public Collection<NumberBike> ReadNumberBikePaged(int pageSize, int pageIndex, string strFilter, string strOrderBy, out int totalRecord)
        {
            totalRecord = 0;
            var dataAccess = new NumberBikeDataAccess();
            try
            {
                if (pageIndex >= 0 && pageSize > 0)
                {
                    var result = dataAccess.ReadNumberBikePaged(pageSize, pageIndex, strFilter, strOrderBy, out totalRecord);
                    if (result != null)
                    {
                        return result;
                    }
                    return null;
                }
                return null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }


        /// <summary>
        /// Insert NumberBike
        /// </summary>
        /// <param name="numberBike"></param>
        /// <returns></returns>
        public int InsertNumberBike(NumberBike numberBike)
        {
            if (string.IsNullOrEmpty(numberBike.NumberPlate)) return -1;
            NumberBikeDataAccess numberBikeDao = new NumberBikeDataAccess();
            try
            {
                return numberBikeDao.InsertNumberBike(numberBike);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        /// <summary>
        /// Get All Number Bike
        /// </summary>
        /// <returns></returns>
        public Collection<NumberBike> GetAllNumberBike()
        {
            var grpDao = new NumberBikeDataAccess();
            try
            {
                var grlList = new Collection<NumberBike>();
                grlList = grpDao.GetAllNumberBike();
                return grlList != null ? grlList : null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }


        public NumberBike GetNumberBikeById(int numberBikeId)
        {
            var grpDao = new NumberBikeDataAccess();
            try
            {
                var grlList = new NumberBike();
                grlList = grpDao.GetByNumberBikeId(numberBikeId);
                return grlList != null ? grlList : null;
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return null;
        }

        public int UpdateNumberBike(NumberBike numberBike)
        {
            if (string.IsNullOrEmpty(numberBike.NumberPlate)) return -1;
            var numberBikeDao = new NumberBikeDataAccess();
            try
            {
                return numberBikeDao.UpdateNumberBike(numberBike);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }

        public int DeleteNumberBike(NumberBike numberBike)
        {
            if (numberBike.NumberBikeId <= 0) return -1;
            var numberBikeDao = new NumberBikeDataAccess();
            try
            {
                return numberBikeDao.DeleteNumberBike(numberBike);
            }
            catch (SqlException exception)
            {
                Console.WriteLine(exception.ToString());
            }
            return -1;
        }
    }
}
